package wx.controls
{
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	
	import wx.core.ITheme;
	import wx.core.SettingCache;

	/**
	 * 
	 * @author wujianqi
	 * 按钮控件
	 * 
	 */	
	public class Button extends ButtonBase
	{
		
		/**
		 *按钮文字标签 
		 */		
		private var _text:String;
		private var _label:Label;
		
		/**
		 *文字标签偏移 
		 */		
		private var _labelOffset:Object;
		
		/**
		 *按钮图标 
		 */		
		private var _icon:Bitmap;
		
		private const _theme:ITheme = SettingCache.theme;
		
		public function Button($label:String="")
		{
			var up:DisplayObject =  _theme.buttonUpSkin;
			var hover:DisplayObject = _theme.buttonHoverSkin;
			var down:DisplayObject = _theme.buttonDownSkin;
			var disabled:DisplayObject = _theme.buttonDisableSkin;
			super(up,hover,down,disabled);
			
			_labelOffset = _theme.buttonLabelOffset;
			_text = $label;	
			addLabel();
		}

		
		public function get icon():Bitmap
		{
			return _icon;
		}

		public function set icon(value:Bitmap):void
		{
			if(_icon != value){
				_icon = value;
				updateDisplay();
			}
		}

		public function get label():String
		{
			return _text;
		}

		public function set label(value:String):void
		{
			if(_text != value){
				_text = value;
				updateDisplay();
			}
		}	
		public function get labelOffset():Object
		{
			return _labelOffset;
		}
		
		public function set labelOffset(value:Object):void
		{
			if(_labelOffset != value){
				_labelOffset = value;
				updateDisplay();
			}
		}
		
		
		/**
		 *添加文字及图标层 
		 * 
		 */		
		private function addLabel():void{
			if(_text!=""){				
				_label = new Label(_text);
				addChild(_label);
				
				if(_labelOffset!=null){
					if(_icon!=null){	
						addChild(_icon);
						_icon.x = (width - _label.width - _labelOffset.left - _icon.width)/2;
						_icon.y = (height - _labelOffset.top - _icon.height)/2;
						_label.x = _icon.x + _icon.width +2;
						_label.y = (height - _label.height - _labelOffset.top)/2;												
					}else{
						_label.x = (width - _label.width - _labelOffset.left)/2;
						_label.y = (height - _label.height - _labelOffset.top)/2;
					}					
				}else{
					if(_icon!=null){
						addChild(_icon);
						_icon.x = (width - _label.width - _icon.width)/2;
						_icon.y = (height - _icon.height)/2;
						_label.x = _icon.x + _icon.width +2;
						_label.y = (height - _label.height)/2;						
					}else{
						_label.x = (width - _label.width)/2;
						_label.y = (height - _label.height)/2;
					}
					
				}
			}
		}
		
		/**
		 *局部重绘更新 
		 * 
		 */		
		private function updateDisplay():void
		{
			if(_icon && contains(_icon)) removeChild(_icon);
			if(_label && contains(_label)) removeChild(_label);
			addLabel();
		}
		
		override public function set width(value:Number):void{			
			super.width = value;
			updateDisplay();
		}
		override public function set height(value:Number):void{			
			super.height = value;
			updateDisplay();
		}
		
	}
	
}